//
//  WSCoinRecordsController.m
//  BaseProject
//
//  Created by karong on 2023/8/7.
//

#import "WSCoinRecordsController.h"
#import "CTMediator+WSUserCenterAction.h"
#import "WSDatePickerView.h"
#import "WSCoinRecordsItemController.h"

@interface WSCoinRecordsController () <WSDatePickerViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *btnArray;

@property (nonatomic, strong) NSMutableArray * contentVCArr;
@property (nonatomic, assign) CGFloat contentHeight;
@property (nonatomic, strong) NSString * yearAndMonth;
@property (weak, nonatomic) IBOutlet UILabel *yearAndMonthLabel;
@property (nonatomic, strong) WSCoinRecordsItemController * currentVC;

@end

@implementation WSCoinRecordsController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSDate * date = [NSDate date];
    self.yearAndMonth = WSSF(@"%ld-%ld", date.year, date.month);
    [self viewLayout];
}

- (void)viewLayout {
    [self.scrollView setContentSize:CGSizeMake(kScreenWidth * 2, self.contentHeight)];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self addChildViewControllers];
    });
}

// 添加子控制器
- (void)addChildViewControllers {
    for (int i = 0; i < 2; i ++) {
        UIViewController * baseVC = [self customPageVCWithIndex:i];
        [self.scrollView addSubview:baseVC.view];
        [self addChildViewController:baseVC];
        [self.contentVCArr addObject:baseVC];
    }
    self.currentVC = self.contentVCArr.firstObject;
}

- (UIViewController *)customPageVCWithIndex:(NSInteger)index {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_CoinRecordsItemWithType:index];
    target.view.frame = CGRectMake(WSScreenWidth * index, 0, self.scrollView.bounds.size.width, self.contentHeight);
    return target;
}

// 获取並且添加子控制器
- (void)setContentVCWithIndex:(NSInteger)index animated:(BOOL)animated {
    if(self.contentVCArr.count == 0) {
        animated = NO;
    }
    [self.scrollView setContentOffset:CGPointMake(kScreenWidth * index, 0) animated:animated];
}

- (void)setBtnStatusWithIndex:(NSInteger)index {
    for (UIButton * btn in self.btnArray) {
        btn.selected = NO;
        btn.selected = btn.tag == index;
    }
    self.currentVC = self.contentVCArr[index];
}

- (IBAction)btnAction:(UIButton *)sender {
    [self setBtnStatusWithIndex:sender.tag];
    if (self.scrollView.scrollEnabled) {
        [self setContentVCWithIndex:sender.tag animated:YES];
    }
}
- (IBAction)dayAction:(id)sender {
    WSDatePickerView* datePickerView = [[WSDatePickerView alloc] init];
    datePickerView.delegate = self;
    [datePickerView setDefaultSelectMonth:self.yearAndMonth dateFormat:@"yyy-MM"];
    [datePickerView show];
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    if (scrollView == self.scrollView) {
        CGFloat x = scrollView.contentOffset.x / WSScreenWidth;
        [self setBtnStatusWithIndex:x];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.scrollView) {
        CGFloat x = scrollView.contentOffset.x / WSScreenWidth;
        [self setBtnStatusWithIndex:x];
    }
}

#pragma mark WSDatePickerViewDelegate
- (void)pickerDateView:(BasePickerView *)pickerDateView selectYear:(NSInteger)year selectMonth:(NSInteger)month selectDay:(NSInteger)day {
    NSString * string = WSSF(@"%ld-%ld", (long)year, (long)month);
    self.yearAndMonth = string;
    [[NSNotificationCenter defaultCenter] postNotificationName:WSRefreshRecordsNotification object:@{@"year":@(year), @"month":@(month)}];
}

- (CGFloat)contentHeight {
    if (_contentHeight <= 0) {
        _contentHeight = WSScreenHeight-CGRectGetMinY(self.scrollView.frame);
    }
    return _contentHeight;
}

- (NSMutableArray *)contentVCArr {
    if (!_contentVCArr) {
        _contentVCArr = [[NSMutableArray alloc]init];
    }
    return _contentVCArr;
}

- (void)setYearAndMonth:(NSString *)yearAndMonth {
    _yearAndMonth = yearAndMonth;
    NSDate * date = [NSDate date];
    NSString * string = WSSF(@"%ld-%ld", date.year, date.month);
    if ([yearAndMonth isEqualToString:string]) {
        self.yearAndMonthLabel.text = APPLocalized(@"本月");
    }
    else {
        self.yearAndMonthLabel.text = yearAndMonth;
    }
}
@end
